﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using DBAccess;
using SSPharma.Model;
using SSPharma.Common;


namespace SSPharma.Biz
{
    public class BizMainWindow
    {
        #region Constant

        public const string ROOT_MENU = "RootMenu";

        public const string MENU_IMG_PATH = "../../Resources/Closed_book.png";
        public const string SCR_IMG_PATH = "../../Resources/Screen.png";

        #endregion

        #region Public Methods

        public static List<MenuItem> DSDanhMuc_Menu()
        {
            List<MenuItem> items = new List<MenuItem>();
            MenuItem item;
            string strLastResponse = string.Empty;
            string strMaResponse = string.Empty;
            string strTen_Response = string.Empty;
            string strMaMenu = string.Empty;
            string strTenMenu = string.Empty;
            string strMaMenuCha = string.Empty;
            string strTenForm = string.Empty;
            string strViewModel = string.Empty;
            string strView = string.Empty;
            string strDirectoryViewModel = string.Empty;
            string strDirectoryViews = string.Empty;
            bool bIsForm = false;

            StringBuilder strSQL = new StringBuilder(300);
            strSQL.Append("WITH MenuSet AS ( ");
            strSQL.Append("SELECT r.MaResponse, r.TenResponse, rm.MaMenu, m.TenMenu, m.MaMenuCha");
            strSQL.Append(" FROM HT_Dm_Response r, HT_Nsd_Res nr, HT_Response_Menu rm, HT_Menu m");
            strSQL.Append(" WHERE nr.MaNguoiSD = @MaNSD");
            strSQL.Append(" AND nr.MaResponse = r.MaResponse");
            strSQL.Append(" AND nr.MaResponse = rm.MaResponse");
            strSQL.Append(" AND rm.MaMenu = m.MaMenu ");
            strSQL.Append(" UNION ALL");
            strSQL.Append(" SELECT ms.MaResponse, ms.TenResponse, m.MaMenu, m.TenMenu, m.MaMenuCha FROM HT_Menu m, MenuSet ms WHERE m.MaMenuCha = ms.MaMenu )");
            strSQL.Append(" SELECT ms.MaResponse, ms.TenResponse, ms.MaMenu, ms.TenMenu, ms.MaMenuCha, mf.TenForm, mf.ViewModel, mf.Views,mf.DirectoryViewModel,mf.DirectoryViews,IsForm FROM MenuSet ms LEFT JOIN HT_DM_MenuForm mf");
            strSQL.Append(" ON ms.MaMenu = mf.MaMenu");
            strSQL.Append(" ORDER BY MaResponse, MaMenuCha, TenMenu ASC");

            SQLServer.Param[] Params = new SQLServer.Param[1];
            Params[0] = new SQLServer.Param("@MaNSD", Global.UserID);

            using (DbDataReader dataReader = Global.DataFactory.Reader(strSQL.ToString(), Params))
            {
                if (dataReader.HasRows)
                {
                    while (dataReader.Read())
                    {
                        strMaResponse = dataReader.GetString(0);
                        strTen_Response = dataReader.GetString(1);
                        strMaMenu = dataReader.GetString(2);
                        strTenMenu = dataReader.GetString(3);
                        strMaMenuCha = dataReader.IsDBNull(4) ? string.Empty : dataReader.GetString(4);
                        strTenForm = dataReader.IsDBNull(5) ? string.Empty : dataReader.GetString(5);
                        strViewModel = dataReader.IsDBNull(6) ? string.Empty : dataReader.GetString(6);
                        strView = dataReader.IsDBNull(7) ? string.Empty : dataReader.GetString(7);
                        strDirectoryViewModel = dataReader.IsDBNull(8) ? string.Empty : dataReader.GetString(8);
                        strDirectoryViews = dataReader.IsDBNull(9) ? string.Empty : dataReader.GetString(9);
                        bIsForm = dataReader.IsDBNull(10) ? false : dataReader.GetBoolean(10);
                        if (!strMaResponse.Equals(strLastResponse))
                        {
                            item = new MenuItem();
                            item.MenuID = strMaResponse;
                            item.MenuName = strTen_Response;
                            item.ParentID = ROOT_MENU;
                            item.FormName = string.Empty;
                            item.IconPath = MENU_IMG_PATH;

                            items.Add(item);
                        }

                        item = new MenuItem();
                        item.MenuID = strMaMenu;
                        item.MenuName = strTenMenu;

                        if (string.IsNullOrWhiteSpace(strMaMenuCha))
                        {
                            item.ParentID = strMaResponse;
                        }
                        else
                        {
                            item.ParentID = strMaMenuCha;
                        }
                        item.FormName = strTenForm;
                        item.ViewModel = strViewModel;
                        item.Views= strView;
                        item.IconPath = strTenForm == string.Empty ? MENU_IMG_PATH : SCR_IMG_PATH;
                        item.DirectoryViewModel = strDirectoryViewModel;
                        item.DirectoryViews = strDirectoryViews;
                        item.IsForm = bIsForm;
                        items.Add(item);

                        strLastResponse = strMaResponse;

                    }
                }
            }

            return items;
        }


        #endregion
    }
}
